package co.fitstart.mobile.web.controller.profile;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.entity.user.User;
import co.fitstart.entity.user.UserAddress;
import co.fitstart.mobile.service.UserAddressService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author houtao
 *
 */
@Controller
@RequestMapping("/profile/addresses")
public class ProfileAddressController {
	
	private static final Logger logger = LoggerFactory.getLogger(ProfileAddressController.class);
    
	@Autowired
	private UserAddressService userAddressService;
	
	/**
	 * 我的收货地址列表页
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
    @WechatAuthority
    @MobileDetect
    @Router(targetUrl = "/required-wechat")
    public ModelAndView list(@Secure User user) {
		
        ModelAndView modelAndView = new ModelAndView("profile/address/profile-address-list");
        
        List<UserAddress> userAddresses = userAddressService.listUserAddress(user);
        
        modelAndView.addObject("userAddresses", userAddresses);
        
        modelAndView.addObject("addressType", "profile");
        
        return modelAndView;
    }
	
	/**
	 * 
	 * @param user
	 * @param page
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
	@RequestMapping(value = "", params = "filter", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
	public @ResponseBody String listAddresses(@Secure User user,
											  Page page) {
    	
		Result result = new Result();
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * create profileAddress.
	 * @param user
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
	@RequestMapping(value = "/create", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    public ModelAndView create(@Secure User user) {
		
        ModelAndView modelAndView = new ModelAndView("profile/address/profile-address-create");
        
        modelAndView.addObject("addressType", "profile");

        return modelAndView;
    }
	
	/**
	 * on create.
	 * @param user
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
	@RequestMapping(value = "" , method = RequestMethod.POST)
    @Router(targetUrl = "/required-wechat")
    public @ResponseBody String onCreate(@Secure User user,  UserAddress userAddress, String defaultAddress){
        
		Result result = new Result();
        
		if (defaultAddress != null && defaultAddress.equals("on")) {
			
			userAddress.makeDefault();
		}
        try {
            
        	userAddressService.create(user, userAddress);
        	
        	result.success().message("增加成功");
            
        } catch (Exception e) {
            
        	result.fail("500").message("增加失败");
            
            logger.error("#onCreate() - create fail, exception is [{}]",e);
            
        }
        
        return JsonUtils.toJsonString(result);
    }
	 
	
	/**
	 * edit profileAddress.
	 * @param user
	 * @param id
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    public ModelAndView edit(@Secure User user, @PathVariable("id") Long id) {
		
        ModelAndView modelAndView = new ModelAndView("profile/address/profile-address-edit");
        
        UserAddress userAddress = userAddressService.load(id);
        
        modelAndView.addObject("userAddress", userAddress);
        
        modelAndView.addObject("addressType", "profile");

        return modelAndView;
    }
	
	/**
	 * on edit
	 * @param user
	 * @param id
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
	@RequestMapping(value = "/{id}" , method = RequestMethod.PATCH)
    @Router(targetUrl = "/required-wechat")
    public @ResponseBody String onEdit(@Secure User user, @Preloading("id") UserAddress address, String defaultAddress){
        
		Result result = new Result();
        
		if (defaultAddress != null && defaultAddress.equals("on")) {
			
			address.makeDefault();
		}
        try {
            
        	userAddressService.edit(user, address);
        	
        	result.success().message("修改成功");
            
        } catch (Exception e) {
            
        	result.fail("500").message("修改失败");
            
            logger.error("#onEdit() - edit fail, exception is [{}]",e);
            
        }
        
        return JsonUtils.toJsonString(result);
    }
	
	/**
	 * pretermit profileAddress.
	 * @param user
	 * @param id
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
	@RequestMapping(value = "/{id}/set-default", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    public ModelAndView pretermit(@Secure User user, @PathVariable("id") Long id) {
		
        ModelAndView modelAndView = new ModelAndView("profile/address/profile-address-pretermit");
        
        return modelAndView;
    }
   
	/**
	 * on pretermit
	 * @param user
	 * @param id
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
	@RequestMapping(value = "/{id}/set-default" , method = RequestMethod.PATCH)
    @Router(targetUrl = "/required-wechat")
    public @ResponseBody String onPretermit(@Secure User user, @PathVariable("id") Long id){
        
		Result result = new Result();
        
        try {
            
        	userAddressService.pretermit(user, id);
        	
        	result.success().message("修改默认成功");
            
        } catch (Exception e) {
            
        	result.fail("500").message("修改默认失败");
            
            logger.error("#onPretermit() - pretermit fail, exception is [{}]",e);
            
        }
        
        return JsonUtils.toJsonString(result);
    }
	
	/**
	 * delete profileAddress.
	 * @param user
	 * @param id
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
    @Router(targetUrl = "/required-wechat")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@Secure User user, @PathVariable("id") Long id) {
        ModelAndView modelAndView = new ModelAndView("profile/address/profile-address-delete");
        
        return modelAndView;
	}
	
	/**
	 * on delete
	 * @param user
	 * @param id
	 * @return
	 */
    @WechatAuthority
    @MobileDetect
    @Router(targetUrl = "/required-wechat")
	@RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@Secure User user, @PathVariable("id") Long id){
        
		Result result = new Result();
        
        try {
            
        	userAddressService.delete(id);
        	
        	result.success().message("删除成功");
            
        } catch (Exception e) {
            
        	result.fail("500").message("删除失败");
            
            logger.error("#onDelete() - delete fail, exception is [{}]",e);
            
        }
        
        return JsonUtils.toJsonString(result);
    }
	
}
